//
//  AFNetWorkTool.swift
//  03-swift中网络的封装
//
//  Created by 盼 on 16/4/10.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit
import AFNetworking

//定义结构体封装API接口
struct API {
    
    //线上环境
    static let onlineHost = "https://api.weibo.com/"
    //开发环境
    static let devHost = "https://dev.api.weibo.com/"
    static let authrizon = "oauth2/access_token"
    static let access_token = "2/users/show.json"
    static let homepage = "2/statuses/home_timeline.json"
}


//定义swift中的枚举
enum HttpMethods: String{
    case POST = "POST"
    case GET  = "GET"
}

class AFNetWorkTool: AFHTTPSessionManager {

    //创建单例对象
//    static let tools: AFNetWorkTool = AFNetWorkTool()
    static let sharedTools: AFNetWorkTool = {
       
//        let tools = AFNetWorkTool()
        
        //创建单例对象
        let tools = AFNetWorkTool(baseURL: NSURL(string: API.onlineHost))
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
}

//MARK: 请求登录和注册信息
extension AFNetWorkTool {
    
    func loadAccessToken(code:String,finished:(response: AnyObject?,error: NSError?)->()){
        
        //POST
//        let URLStr = "https://api.weibo.com/oauth2/access_token"
        let grant_type = "authorization_code"
        
        //AFN中以字典的形式传递参数
        let parameters:[String: AnyObject] = ["client_id": client_id,"client_secret":client_secret,"grant_type":grant_type,"code":code,"redirect_uri":redirect_uri]
        
        //核心代码请求数据
        request(.POST, URLStr: API.authrizon, parameters: parameters, finished: finished)
  }
    
    
    func loadUserInfo(parameters:AnyObject,finished:(response: AnyObject?,error: NSError?)->()){
//        let URLStr = "https://api.weibo.com/2/users/show.json"        
        request(.GET, URLStr: API.access_token, parameters: parameters, finished: finished)
    }
}


//MARK:请求微博信息
extension AFNetWorkTool {
//    https://api.weibo.com/2/statuses/home_timeline.json
    
    func loadHomePage(token: String,max_id: Int64,since_id: Int64,finished:(response: AnyObject?,error: NSError?)->()) {
        
        var parameters = ["access_token":token]
        //第一请求的时候max_id = 0，应该进行判断
        if max_id > 0 {
            parameters["max_id"] = "\(max_id-1)"
        }
        
        if since_id > 0 {
            parameters["since_id"] = "\(since_id)"
        }
        
        request(.GET, URLStr: API.homepage, parameters: parameters, finished: finished)
    }
    
}

//MARK:请求网络数据的核心代码
extension AFNetWorkTool {
    //封装请求网络数据的方法
    func request(Methods: HttpMethods,URLStr: String,parameters: AnyObject?,finished:(response: AnyObject?,error: NSError?)->()){
        
        //swift中不能使用黑魔法，但是可以把相同的闭包抽取出来当作参数传递
        
        let success = {(task: NSURLSessionDataTask, responseObject: AnyObject?) -> Void in
            
            finished(response: responseObject,error: nil)
            
        }
        
        let failure = {(task: NSURLSessionDataTask?, error: NSError) -> Void in
            
            finished(response: nil,error: error)
        }
        
        
        if  Methods == .GET {
            
            AFNetWorkTool.sharedTools.GET(URLStr, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            
            AFNetWorkTool.sharedTools.POST(URLStr, parameters: parameters, progress: nil, success: success, failure: failure)
            
        }
    }
}
